1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect.testing.testers;
18
19 import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
20 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21 import static com.google.common.collect.testing.features.MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
22 import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
23
24 import com.google.common.annotations.GwtCompatible;
25 import com.google.common.collect.testing.AbstractMapTester;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28
29 import java.util.ConcurrentModificationException;
30 import java.util.Iterator;
31 import java.util.Map.Entry;
32
33
34
35
36
37
38
39
40
41 @GwtCompatible
42 public class MapClearTester<K, V> extends AbstractMapTester<K, V> {
43 @MapFeature.Require(SUPPORTS_REMOVE)
44 public void testClear() {
45 getMap().clear();
46 assertTrue("After clear(), a map should be empty.",
47 getMap().isEmpty());
48 }
49
50 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
51 SUPPORTS_REMOVE})
52 @CollectionSize.Require(SEVERAL)
53 public void testClearConcurrentWithEntrySetIteration() {
54 try {
55 Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator();
56 getMap().clear();
57 iterator.next();
58 fail("Expected ConcurrentModificationException");
59 } catch (ConcurrentModificationException expected) {
60
61 }
62 }
63
64 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
65 SUPPORTS_REMOVE})
66 @CollectionSize.Require(SEVERAL)
67 public void testClearConcurrentWithKeySetIteration() {
68 try {
69 Iterator<K> iterator = getMap().keySet().iterator();
70 getMap().clear();
71 iterator.next();
72 fail("Expected ConcurrentModificationException");
73 } catch (ConcurrentModificationException expected) {
74
75 }
76 }
77
78 @MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION,
79 SUPPORTS_REMOVE})
80 @CollectionSize.Require(SEVERAL)
81 public void testClearConcurrentWithValuesIteration() {
82 try {
83 Iterator<V> iterator = getMap().values().iterator();
84 getMap().clear();
85 iterator.next();
86 fail("Expected ConcurrentModificationException");
87 } catch (ConcurrentModificationException expected) {
88
89 }
90 }
91
92 @MapFeature.Require(absent = SUPPORTS_REMOVE)
93 @CollectionSize.Require(absent = ZERO)
94 public void testClear_unsupported() {
95 try {
96 getMap().clear();
97 fail("clear() should throw UnsupportedOperation if a map does "
98 + "not support it and is not empty.");
99 } catch (UnsupportedOperationException expected) {
100 }
101 expectUnchanged();
102 }
103
104 @MapFeature.Require(absent = SUPPORTS_REMOVE)
105 @CollectionSize.Require(ZERO)
106 public void testClear_unsupportedByEmptyCollection() {
107 try {
108 getMap().clear();
109 } catch (UnsupportedOperationException tolerated) {
110 }
111 expectUnchanged();
112 }
113 }